17 state(int I
, int L
, int W
) : i(I
), lights(L
), w(W
) {}
22 int toggleBit(int x
, int b
){
29 int differentBit(int x
, int y
){
30 return __builtin_ctz(x
^ y
);
35 while (scanf("%d%d%d", &r
, &d
, &s
) == 3 && (r
+d
+s
)){
36 vector
<int> doors
[r
], switches
[r
];
37 printf("Villa #%d\n", C
++);
39 for (int i
=0; i
<d
; ++i
){
41 scanf("%d%d", &u
, &v
);
43 doors
[u
].push_back(v
);
44 doors
[v
].push_back(u
);
47 for (int i
=0; i
<s
; ++i
){
49 scanf("%d%d", &u
, &v
);
51 switches
[u
].push_back(v
);
54 bool visited
[(1<<MAXR
)+1][MAXR
] = {false};
57 q
.push(state(0, 1<<0, 0));
61 //printf("popped state: i = %d, lights = %X, w = %d\n", u.i, u.lights, u.w);
63 if (u
.i
== r
- 1 && u
.lights
== (1 << (r
-1))){
66 //printf("u.path.size() = %d, u.w = %d\n", u.path.size(), u.w);
67 assert(u
.path
.size() == u
.w
);
68 //printf("Solution found. i = %d, lights = %X, w = %d\n", u.i, u.lights, u.w);
69 printf("The problem can be solved in %d steps:\n", u
.w
);
70 copy(u
.path
.begin(), u
.path
.end(), ostream_iterator
<string
>(cout
, ""));
74 if (visited
[u
.lights
][u
.i
]) continue;
75 visited
[u
.lights
][u
.i
] = true;
77 vector
<int> &s
= switches
[u
.i
];
78 for (int j
=0; j
<s
.size(); ++j
){
79 int nlights
= toggleBit(u
.lights
, s
[j
]);
80 if (!visited
[nlights
][u
.i
] && (nlights
& (1 << u
.i
))){
81 q
.push(state(u
.i
, nlights
, u
.w
+ 1));
83 sprintf(buf
, "- Switch %s light in room %d.\n", (nlights
> u
.lights
? "on" : "off"), differentBit(u
.lights
, nlights
) + 1);
84 q
.back().path
= u
.path
;
85 q
.back().path
.push_back(string(buf
));
86 //printf(" pushed state: i = %d, lights = %X, w = %d\n", u.i, nlights, u.w + 1);
90 vector
<int> &d
= doors
[u
.i
];
91 for (int j
=0; j
<d
.size(); ++j
){ //Moverme sin apagar luces
92 if (!visited
[u
.lights
][d
[j
]] && (u
.lights
& (1 << d
[j
]))){
93 q
.push(state(d
[j
], u
.lights
, u
.w
+ 1));
95 sprintf(buf
, "- Move to room %d.\n", d
[j
] + 1);
96 //printf(" pushed state: i = %d, lights = %X, w = %d\n", d[j], u.lights, u.w + 1);
97 q
.back().path
= u
.path
;
98 q
.back().path
.push_back(string(buf
));
103 if (!solved
) printf("The problem cannot be solved.\n");